package com.example.my.agiftthat.ui.home.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;

import com.example.my.agiftthat.R;
import com.example.my.agiftthat.config.Url;
import com.example.my.agiftthat.helper.GlideImageLoader;
import com.example.my.agiftthat.helper.OkHttpClientHelper;
import com.example.my.agiftthat.helper.decoration.SpacesItemDecoration;
import com.example.my.agiftthat.ui.home.adapter.LowRecyclerAdapter;
import com.example.my.agiftthat.ui.home.adapter.SendRecyclerAdapter;
import com.example.my.agiftthat.ui.home.adapter.SixRecyclerAdapter;
import com.example.my.agiftthat.ui.home.bean.BannerBean;
import com.example.my.agiftthat.ui.home.bean.LowBean;
import com.example.my.agiftthat.ui.home.bean.SendBean;
import com.example.my.agiftthat.ui.home.bean.SixBean;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by My on 2016/12/3.
 */

public class SendFragment extends Fragment {
    private Context mContext = null;
    private RecyclerView recyclerView_send;
    private PullToRefreshScrollView pullToRefreshScrollView_send;
    private List<SendBean.DataBean.ItemsBean> sList = new ArrayList<>();
    private SendRecyclerAdapter adapter = null;
    private int curPage = 1;
    private String url = null;
    private Handler handler2 = new Handler() {
        @Override
        public void handleMessage(Message msg2) {
            super.handleMessage(msg2);
            switch (msg2.what) {
                case 2:
                    sList = (List<SendBean.DataBean.ItemsBean>) msg2.obj;
                    if (curPage == 1) {
                        adapter.reloadRecyclerView(sList, true);
                    } else {
                        adapter.reloadRecyclerView(sList, false);
                    }
                    pullToRefreshScrollView_send.onRefreshComplete();
                    break;
            }
        }
    };

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        reloadNetworkData1();
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_send, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView_send = (RecyclerView) view.findViewById(R.id.recyclerView_send);
        pullToRefreshScrollView_send = (PullToRefreshScrollView) view.findViewById(R.id.pullToRefreshScrollView_send);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        recyclerView_send.setHasFixedSize(true);
        recyclerView_send.addItemDecoration(new SpacesItemDecoration(4));
        recyclerView_send.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new SendRecyclerAdapter(mContext, sList);
        recyclerView_send.setAdapter(adapter);
//        Bundle bundle=getArguments();
//       final String path=bundle.getString("url");
//        reloadNetworkData1();
        pullToRefreshScrollView_send.setMode(PullToRefreshBase.Mode.BOTH);
        pullToRefreshScrollView_send.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                curPage = 1;
                reloadNetworkData1();
                pullToRefreshScrollView_send.onRefreshComplete();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                curPage++;
                reloadNetworkData1();
            }
        });
    }

    private void reloadNetworkData1() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
//                    if (curPage == 1) {
                    String jsonStr = OkHttpClientHelper.getStringFromURL(mContext, Url.HOME_SNP, 3);
                    Gson gson = new Gson();
                    SendBean sendBean = gson.fromJson(jsonStr, SendBean.class);
                    sList = sendBean.getData().getItems();
                    url = sendBean.getData().getPaging().getNext_url();
                    Message msg2 = new Message();
                    msg2.what = 2;
                    msg2.obj = sList;
                    handler2.sendMessage(msg2);
//                    } else if(curPage!=1){
//                        String jsonStr = OkHttpClientHelper.getStringFromURL(mContext, url, 4);
//                        Gson gson = new Gson();
//                        LowBean lowBean = gson.fromJson(jsonStr, LowBean.class);
//                        lList = lowBean.getData().getItems();
//                        url = lowBean.getData().getPaging().getNext_url();
//                        Message msg2 = new Message();
//                        msg2.what = 2;
//                        msg2.obj = lList;
//                        handler2.sendMessage(msg2);
//                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
